package com.wondertek.poms.core.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.common.utils.CopyUtils;
import com.wondertek.poms.core.service.IProductInfoPackageService;
import com.wondertek.poms.dao.po.ProductInfoPackage;
import com.wondertek.poms.dao.pojo.ProductInfoPackagePojo;
import com.wondertek.poms.dao.repo.IProductInfoPackageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;


/**
 * <p><b>ProductInfoPackageServiceImpl Description:</b> 二级产品信息 service层实现 </p>
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-19 15:14:14</p>
 */
@Service
@Scope("prototype")
public class ProductInfoPackageServiceImpl extends ServiceImpl<ProductInfoPackage, Long> implements IProductInfoPackageService {

	@Autowired
	private IProductInfoPackageDao productInfoPackageDao;
	@PersistenceContext
    public EntityManager entityManager;

	@Override
	@Transactional
	public ProductInfoPackage deleteLogic(Long id) {
		//TODO (如果报错删除方法体)
		ProductInfoPackage cp = jpaRepository.getOne(id);
		ProductInfoPackage.putTimeAndUser(cp);
		jpaRepository.save(cp);
		return cp;
	}

	@Override
	@Transactional
	public ProductInfoPackage merge(ProductInfoPackage model) {
		//TODO (如果报错删除报错行)
		ProductInfoPackage.putTimeAndUser(model);
		ProductInfoPackage dbo = jpaRepository.getOne(model.getProductInfoPackageId());
		CopyUtils.copyProperties(model, dbo);
		return jpaRepository.save(dbo);
	}

	@Override
	public List<ProductInfoPackage> findPrdInfoPackageByFirstPrd(Long firstProductInfoPackageId) {
		return productInfoPackageDao.findPrdInfoPackageByFirstPrd(firstProductInfoPackageId);
	}

	/**
	 * 获取所有二级产品
	 * @return
	 */
	@Override
	public List<ProductInfoPackage> getAllProductInfoPackage() {
		return productInfoPackageDao.findAllProductInfoPackage();
	}

	@Override
	public List<ProductInfoPackagePojo> findAll(ProductInfoPackagePojo entity, int page, int rows, String sord, String sidx) {
		List<ProductInfoPackagePojo> list = new ArrayList<>();
		StringBuilder condition = new StringBuilder();
		condition.append("select b.FIRST_PRODUCT_NAME,a.PRODUCT_INFO_PACKAGE_ID,a.PRODUCT_INFO_PACKAGE_NAME,a.TYPE,a.CREATE_DATE,a.LAST_UPDATE_DATE,a.LAST_OPERATOR " +
				"from PRODUCT_INFO_PACKAGE a left join FIRST_PRODUCT_INFO_PACKAGE b on a.FIRST_PRODUCT_INFO_PACKAGE_ID=b.FIRST_PRODUCT_ID order by a.LAST_UPDATE_DATE desc");
		Object o = entityManager
				.createNativeQuery("select count(*) from ( " + condition.toString() + " ) a").getSingleResult();
		int count = Convert.toInt(o);
		Pageable pageable = getPageable(page - 1, rows, sord, sidx);

		if (count > 0) {

			Query query = entityManager.createNativeQuery(condition.toString());
			/*query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
			query.setMaxResults(pageable.getPageSize());*/
			List<Object[]> resultList = query.getResultList();
			list = getList(resultList);
			//PageImpl<ProductInfoPackagePojo> productInfoPackagePojo = new PageImpl<>(list, null, count);

			entityManager.close();
			return list;
		}
		entityManager.close();
		return list;
	}

	@Override
	public Long getFirstProductInfoIdByName(String name) {
		Object ob=entityManager
				.createNativeQuery("select FIRST_PRODUCT_ID from FIRST_PRODUCT_INFO_PACKAGE where FIRST_PRODUCT_NAME= '"+name+"'").getSingleResult();
		Long firstProductInfoId=Convert.toLong(ob);
		return firstProductInfoId;
	}

	@Override
	public List<ProductInfoPackage> getAllPomsProductInfoPackage() {
		return productInfoPackageDao.getAllPomsProductInfoPackage();
	}

	private List<ProductInfoPackagePojo> getList(List<Object[]> rows) {
		List<ProductInfoPackagePojo> list = new ArrayList<>();
		for (Object[] o : rows) {
			ProductInfoPackagePojo temp = new ProductInfoPackagePojo();
			temp.setFirstProductInfoPackageName(Convert.toStr(o[0]));
			temp.setProductInfoPackageId(Convert.toLong(o[1]));
			temp.setProductInfoPackageName(Convert.toStr(o[2]));
			temp.setType(Convert.toStr(o[3]));
			temp.setCreateDate(DateUtil.format(Convert.toLocalDateTime(o[4]),"yyyy-MM-dd HH:mm:ss"));
			temp.setLastUpdateDate(DateUtil.format(Convert.toLocalDateTime(o[5]),"yyyy-MM-dd HH:mm:ss"));
			temp.setLastOperator(Convert.toStr(o[6]));
			list.add(temp);
		}
		return list;
	}

	private StringBuilder sqlStringWhere(ProductInfoPackagePojo productInfoPackage){
		StringBuilder condition = new StringBuilder();
		String sql="select b.FIRST_PRODUCT_NAME,a.PRODUCT_INFO_PACKAGE_ID,a.PRODUCT_INFO_PACKAGE_NAME,a.TYPE,a.CREATE_DATE,a.LAST_UPDATE_DATE,a.LAST_OPERATOR " +
				"from PRODUCT_INFO_PACKAGE a left join FIRST_PRODUCT_INFO_PACKAGE b on a.FIRST_PRODUCT_INFO_PACKAGE_ID=b.FIRST_PRODUCT_ID where 1=1";
		condition.append(sql);

		if(StrUtil.isNotBlank(productInfoPackage.getProductInfoPackageName())){
			condition.append(" and a.PRODUCT_INFO_PACKAGE_NAME like ").append("'%"+productInfoPackage.getProductInfoPackageName()+"%'");
		}
		if(StrUtil.isNotBlank(productInfoPackage.getFirstProductInfoPackageName())){
			condition.append(" and b.FIRST_PRODUCT_NAME like ").append("'%"+productInfoPackage.getFirstProductInfoPackageName()+"%'");
		}
		if(StrUtil.isNotBlank(productInfoPackage.getFirstProductInfoPackageId())){
			condition.append(" and b.first_product_info_package_id =").append(productInfoPackage.getFirstProductInfoPackageId());
		}
		if(StrUtil.isNotBlank(productInfoPackage.getType())){
			condition.append(" and a.TYPE =").append("'"+productInfoPackage.getType()+"'");
		}
		condition.append(" order by a.LAST_UPDATE_DATE desc");
		return condition;
	}
}